
local RechargeItem = {};
HelperFunc.SetUIEnv(RechargeItem)
local this = RechargeItem;

this.ranklist = nil;
this.isClick = false;
this.challengeTimeCountDown = 1;
this.activityId = 19001;

function RechargeItem.initData(id, obj, challengeTimeCountDown)
    ---@type GList
    this.activityId = tonumber(id);
    this.ranklist = obj:GetChild("List");
    this.ranklist.itemRenderer = this.updateItem;
    this.ranklist:SetVirtual();
    local cfgs = dataTable.getDataTable("t_periodtask")
    this.cfgList = {}
    for k,v in pairs(cfgs) do
        if v.f_ActivityId == this.activityId and v.f_TurntableTask == 1 then
            table.insert(this.cfgList,v)
        end
    end
    table.sort(this.cfgList,function(a,b)
        return a.f_ActTaskId < b.f_ActTaskId
    end)
    this.updateData(challengeTimeCountDown);
end
function RechargeItem.OnDestroy()
    this.cfgList = nil
end
--0:未完成，1：完成未领，2：完成已领
function RechargeItem.GetRewardState(id)

    local data = logicMgr.ActiveManager.GetTurntableTaskData(this.activityId,id)
    if data and data.recvStatus == 1 then
        return 2
    end
    local cfg = dataTable.getDataTableByStringId("t_periodtask",id)
    if(data and data.process >= cfg.f_Progress) then
        return 1
    end
    return 0
end
function RechargeItem.Sort()
    local stateList = {}
    for k,v in pairs(this.cfgList) do
        stateList[v.f_ActTaskId] = this.GetRewardState(v.f_ActTaskId)
    end
    table.sort(this.cfgList,function(a,b)
        local aState = stateList[a.f_ActTaskId]
        local bState = stateList[b.f_ActTaskId]
        local aw = aState == 1 and 10 or 1
        local bw = bState == 1 and 10 or 1
        if aw ~= bw then
            return aw > bw
        end
        aw = aState == 0 and 10 or 1
        bw = bState == 0 and 10 or 1
        if aw ~= bw then
            return aw > bw
        end
        return a.f_ActTaskId < b.f_ActTaskId
    end)
end
function RechargeItem.updateData(timeCountDown)
    this.challengeTimeCountDown = timeCountDown or 1;
    this.Sort()
    this.ranklist.numItems = #this.cfgList
    this.isClick = false;
end

function RechargeItem.updateItem(index, itemObj)
    index = index + 1
    local statusCtrl = itemObj:GetController("status");
    local title = itemObj:GetChild("title")
    local tips = itemObj:GetChild("Tips")
    local progress = itemObj:GetChild("n10");
    local btnGo = itemObj:GetChild("btn_Go");
    local btnGet = itemObj:GetChild("btn_Get");
    local effect = itemObj:GetChild("effect_tzsl")
    local cfg = this.cfgList[index]
    logicMgr.CostManager.RefreshItemList(itemObj:GetChild("itemList"), cfg.f_Award);
    local data = logicMgr.ActiveManager.GetTurntableTaskData(this.activityId,cfg.f_ActTaskId)
    local process = 0
    if data then
        process = data.process
    end
    title.text = getLanguage(cfg.f_ActTaskDesc)
    tips.text = string.format(getLanguage("ActivityDesc1100304"),math.min(process,cfg.f_Progress),cfg.f_Progress)
    local state = this.GetRewardState(cfg.f_ActTaskId)
    statusCtrl.selectedIndex = state
    progress.value = process/cfg.f_Progress*100
    btnGo.onClick:Clear();
    btnGet.onClick:Clear();
    if state == 0 then --未完成
        --btnGo.visible = cfg.f_Switchover ~= 0
        --effect.visible = cfg.f_Switchover ~= 0
        --if cfg.f_Switchover > 0 then
            btnGo.onClick:Add(function()
                if this.isClick then
                    return;
                end
                this.isClick = true
                this.DelayEnableClick()
                closeUI(PanelNames.UIActivityLuckyGuy)
                closeUI(PanelNames.UIActivity)
                showUI(PanelNames.UIHeadMenu);
                --logicMgr.FuncOpenManager.jump(cfg.f_Switchover)
                if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.Cost, true) then
                    logicMgr.HeroManager.setGetNewHeroShow();
                    logicMgr.CostManager.gotoCostById();
                end
            end)
        --end
        if this.challengeTimeCountDown < 0 then
            statusCtrl.selectedIndex = 3;
        end
    elseif state == 1 then--未领取
        btnGet.onClick:Add(function()
            if this.isClick then
                return;
            end
            this.isClick = true
            this.DelayEnableClick()
            ActivityHandler.sendReqRecvTurntableTaskAwardMessage(this.activityId,cfg.f_ActTaskId)
        end)
    end
end
function RechargeItem.DelayEnableClick()
    utimer.delayCall(this.EnableClick,1000)
end
function RechargeItem.EnableClick()
    this.isClick = false
end
return RechargeItem;